//
//  joinQuestDetailsTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/23.
//

#import "joinQuestDetailsTableViewCell.h"
#import "SYAudio.h"

@interface joinQuestDetailsTableViewCell ()

@property (nonatomic ,strong) UILabel * taskName;
@property (nonatomic ,strong) UILabel * statusName;
@property (nonatomic ,strong) UILabel * pondName;
@property (nonatomic ,strong) UILabel * executeTime;
@property (nonatomic ,strong) UILabel * createTime;

@property (nonatomic ,strong) UILabel * reason;
///语音
@property (nonatomic ,strong) UIView * yyview;
@property (nonatomic ,strong) UILabel * yytime;

@property (nonatomic ,strong) UILabel * userName;
@property (nonatomic ,strong) UILabel * handover;


@property (nonatomic,strong) NSTimer * timer;
@property (nonatomic, assign) UIImageView * yyimg;
@property (nonatomic, assign) int timeS;
@end
@implementation joinQuestDetailsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIView * backview = [UIView new];
    [self.contentView addSubview:backview];
    backview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(5) andView:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.offset(kwt(-10));
    }];

    
    UILabel * statusName = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:UIColor.whiteColor];
    self.statusName= statusName;
    statusName.textAlignment = NSTextAlignmentCenter;
    [backview addSubview:statusName];
    [statusName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.width.offset(kwt(58));
        make.height.offset(kwt(25));
        make.top.offset(kwt(12));
    }];
    
    
    UILabel * taskName = [DTKTool labelWithText:@"--" andFont:kCFont(19) andTextColor:KcolorHex(@"#222222")];
    self.taskName= taskName;
    [backview addSubview:taskName];
    [taskName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.right.equalTo(statusName.mas_left).offset(kwt(-10));
        make.top.offset(kwt(15));
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#DBDBDB");
    [backview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(49));
        make.right.offset(kwt(-12));
        make.left.offset(kwt(12));
        make.height.offset(0.5);
    }];
    
    UILabel * pondName = [DTKTool labelWithText:@"--" andFont:kCFont(16) andTextColor:KcolorHex(@"#222222")];
    self.pondName= pondName;pondName.numberOfLines = 0;
    [backview addSubview:pondName];
    [pondName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.left.offset(kwt(12));
        make.top.offset(kwt(60));
    }];
    
    
    UILabel * templab = [DTKTool labelWithText:@"执行时间：" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [backview addSubview:templab];
    [templab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(pondName.mas_bottom).offset(kwt(12));
        make.height.offset(kwt(16));
    }];
    
    UILabel * executeTime = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.executeTime= executeTime;
    [backview addSubview:executeTime];
    [executeTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab.mas_right);
        make.centerY.equalTo(templab);
    }];

    
    UILabel * templab1 = [DTKTool labelWithText:@"转交原因：" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [backview addSubview:templab1];
    [templab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(templab.mas_top).offset(kwt(28));
        make.height.offset(kwt(16));
    }];
    
    UILabel * reason = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.reason= reason;
    [backview addSubview:reason];
    [reason mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab1.mas_right);
        make.centerY.equalTo(templab1);
    }];
   
    UIView * yyview = [UIView new];
    yyview.clipsToBounds = YES;
    self.yyview = yyview;
    [backview addSubview:yyview];
    [yyview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(templab1.mas_bottom);
        make.height.offset(kwt(46));
    }];
    
    
    UILabel * templab2 = [DTKTool labelWithText:@"备注：" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [yyview addSubview:templab2];
    [templab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.centerY.offset(kwt(5));
    }];
    
    UIView * yybtn = [UIView new];
    [DTKTool YJ:kwt(5) andView:yybtn];
    yybtn.backgroundColor = KcolorHex(@"#29B560");
    [yyview addSubview:yybtn];
    [yybtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(65));
        make.centerY.offset(kwt(5));
        make.width.offset(kwt(114));
        make.height.offset(kwt(36));
    }];
    MJWeakSelf
    yybtn.addClickBlock(^{
        [[SYAudio shareAudio] audioPlayWithURL:weakSelf.model.voiceUrl];
    });
    
    
    UILabel * yytime = [DTKTool labelWithText:@"0\"" andFont:kCFont(16) andTextColor:UIColor.whiteColor];
    [yybtn addSubview:yytime];
    self.yytime = yytime;
    [yytime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(36));
        make.centerY.offset(0);
    }];
    
    UIImageView * yyimg = kimageView(@"language_playback0");
    [yybtn addSubview:yyimg];
    self.yyimg = yyimg;

    [yyimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yytime.mas_right).offset(kwt(6));
        make.width.height.offset(kwt(16));
        make.centerY.offset(0);
    }];
    
    UIView * xxview = [UIView new];
    xxview.backgroundColor = KcolorHex(@"#F6F7F9");
    [backview addSubview:xxview];
    [DTKTool YJ:kwt(5) andView:xxview];
    [xxview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.top.equalTo(yyview.mas_bottom).offset(kwt(10));
        make.height.offset(kwt(96));
    }];
    
    
    UIView * sxlab = [UIView new];
    sxlab.backgroundColor = KcolorHex(@"#CECECE");
    [xxview addSubview:sxlab];
    [sxlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(23));
        make.width.offset(kwt(1));
        make.left.offset(kwt(14));
        make.height.offset(kwt(22));
    }];
   
    UIView * zjrview = [UIView new];
    zjrview.backgroundColor = KcolorHex(@"#FF8800");
    [DTKTool YJ:kwt(4) andView:zjrview];
    [xxview addSubview:zjrview];
    [zjrview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(15));
        make.height.width.offset(kwt(8));
        make.centerX.equalTo(sxlab);
    }];
    
    UILabel * templab3 = [DTKTool labelWithText:@"转交人：" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [xxview addSubview:templab3];
    [templab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(27));
        make.centerY.equalTo(zjrview);
    }];
    
    UILabel * userName = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.userName= userName;
    [xxview addSubview:userName];
    [userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab3.mas_right);
        make.centerY.equalTo(templab3);
    }];
    
    
    UIView * jsrview = [UIView new];
    jsrview.backgroundColor = KcolorHex(@"#02BD4B");
    [DTKTool YJ:kwt(4) andView:jsrview];
    [xxview addSubview:jsrview];
    [jsrview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zjrview.mas_bottom).offset(kwt(21));
        make.height.width.offset(kwt(8));
        make.centerX.equalTo(sxlab);
    }];
    
    UILabel * templab4 = [DTKTool labelWithText:@"接收人：" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [xxview addSubview:templab4];
    [templab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(27));
        make.centerY.equalTo(jsrview);
    }];
    
    UILabel * handover = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.handover= handover;
    [xxview addSubview:handover];
    [handover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab4.mas_right);
        make.centerY.equalTo(templab4);
    }];
    
    UILabel * templab5 = [DTKTool labelWithText:@"申请时间：" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [xxview addSubview:templab5];
    [templab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(kwt(69));
    }];
    UILabel * createTime = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.createTime= createTime;
    [xxview addSubview:createTime];
    [createTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab5.mas_right);
        make.centerY.equalTo(templab5);
    }];
   
    
}

- (void)opentimeWithTimes:(int)times{
    if(self.timer.isValid){
        [_timer invalidate];
    }
    _timer=nil;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1/3.0 target:self selector:@selector(openAudio) userInfo:nil repeats:YES];
    self.timeS = times * 3;
}
- (void)openAudio{
    self.timeS --;
    self.yyimg.image = [UIImage imageNamed: [NSString stringWithFormat:@"language_playback%d",self.timeS%3]];
    if(self.timeS < 0){
        if(self.timer.isValid){
            [_timer invalidate];
        }
        _timer=nil;
        self.yyimg.image = [UIImage imageNamed: [NSString stringWithFormat:@"language_playback0"]];
    }
}
- (void)setModel:(joinQuestListModel *)model {
    _model = model;
    self.yyimg.image = [UIImage imageNamed: [NSString stringWithFormat:@"language_playback0"]];
    self.taskName.text = model.taskName;
    self.statusName.text = model.statusName;
    self.statusName.textColor = KcolorHex(model.statusColor);
    [DTKTool YJBK:kwt(4) andView:self.statusName andWidth:1 andColor:KcolorHex(model.statusColor)];
    self.pondName.text = model.tissueListStr;// [NSString stringWithFormat:@"%@ %@",model.pondTissue,model.pondName];
    self.executeTime.text = model.executeTime;
    self.userName.text = model.userName;
    self.handover.text = model.handover;
    self.createTime.text = model.createTime;
    self.reason.text = model.reason;

    if(model.voiceUrl.length > 0){
        [self.yyview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(kwt(46));
        }];
        self.yytime.text = model.voiceDuration;
    } else {
        [self.yyview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
    }
}

@end
